import url from '@/common/api'
import defaultAvatar from '@/assets/images/base/ic_default_portrait.png'
import personDefaultAvatar from '@/assets/images/base/ic_default_portrait.png'

export function getImgPath(file) {
  if (file) {
    try {
      if (JSON.parse(file).id) {
        return url.imgPath + JSON.parse(file).id
      } else if (JSON.parse(file).data) {
        return url.imgPath + JSON.parse(file).data[0].id
      } else {
        return url.imgPath + JSON.parse(file)[0].id
      }
    } catch (e) {
      return url.imgPath + file
    }
  } else {
    return ''
  }
}

export function getAvatarPath(file) {
  if (file) {
    try {
      if (JSON.parse(file).id) {
        return url.base.imgPath + JSON.parse(file).id
      } else if (JSON.parse(file).data) {
        return url.base.imgPath + JSON.parse(file).data[0].id
      } else {
        return url.base.imgPath + JSON.parse(file)[0].id
      }
    } catch (e) {
      return url.base.imgPath + file
    }
  } else {
    return defaultAvatar
  }
}

export function getPersonAvatarPath(file) {
  if (file) {
    try {
      if (JSON.parse(file).id) {
        return url.base.imgPath + JSON.parse(file).id
      } else if (JSON.parse(file).data) {
        return url.base.imgPath + JSON.parse(file).data[0].id
      } else {
        return url.base.imgPath + JSON.parse(file)[0].id
      }
    } catch (e) {
      return url.base.imgPath + file
    }
  } else {
    return personDefaultAvatar
  }
}

/*
*处理上万的数据 显示格式
* 如果是 不足1万 ， 则显示具体数字
*       刚好1万整数， 则显示 1w
*       1万01  则显示 1.00w+
*       1万9千9百   则显示1.99w
*       1万9千9百 01  则显示1.99W+
*
* */
export function toDecimal(num) {
  if (+num > 10000) {
    var s = (num / 10000).toString()
    let numList = []
    let str = ''
    if (s.indexOf('.') !== -1) {
      numList = s.split('.')
    } else {
      numList.push(s)
    }
    if (numList.length > 0) {
      if (numList.length === 1) {
        str = numList[0] + 'w'
      } else {
        for (let i = 0; i < numList.length; i++) {
          if (i === 0) {
            str = numList[0] + '.'
          }
          if (i === 1) {
            if (numList[i].length >= 2) {
              const numLength = numList[i].length

              str += numList[i].substring(0, 2)
              if (numLength > 2 && parseFloat(numList[i].substring(2, numLength)) >= 1) {
                str += 'w+'
              } else {
                str += 'w'
              }
            } else {
              str += numList[i]
              if (parseFloat(numList[i]) >= 1) {
                str += 'w+'
              } else {
                str += 'w'
              }
            }
          }
        }
      }
    }
    return str
  } else {
    return num
  }
}

/**
 * 按照规则格式化时间
 * @param time
 * @param cFormat
 * @returns {*}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) {
      time = parseInt(time) * 1000
    }
    time = time.replace(/-/g, '/')
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

/**
 * 以返回当前时间的前多长时间格式化
 * @param time
 * @param option
 * @returns {*}
 */

export function formatTime(time, option) {
  if (!time) {
    return
  }
  /* time = +time * 1000*/
  const d = new Date(time.replace(/-/g, '/'))
  const now = Date.now()
  const diff = (now - d) / 1000
  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getFullYear() + '年' +
      (d.getMonth() + 1) +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

/**
 * 格式化数字， 每隔3位添加逗号
 * @param num
 * @returns {string}
 */
export function toThousands(num) {
  return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
}

export function fomatterTime(t) {
  const fTime = t || 0
  let time = parseInt(fTime) + '秒'
  if (parseInt(fTime) > 60) {
    var second = parseInt(fTime) % 60
    var min = parseInt(fTime / 60)
    time = min + '分' + second + '秒'

    if (min > 60) {
      min = parseInt(fTime / 60) % 60
      var hour = parseInt(parseInt(fTime / 60) / 60)
      time = hour + '小时' + min + '分' + second + '秒'

      if (hour > 24) {
        hour = parseInt(parseInt(fTime / 60) / 60) % 24
        var day = parseInt(parseInt(parseInt(fTime / 60) / 60) / 24)
        time = day + '天' + hour + '小时' + min + '分' + second + '秒'
      }
    }
  }
  return time
}

